package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.mapper.DishMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/admin/dish")
@Slf4j
@Api(tags = "菜品接口")
public class DishController {

    @Autowired
    private DishService dishService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private DishMapper dishMapper;

    /**
     * 新增菜品
     *
     * @param dishDTO
     * @return
     */
    @PostMapping
    @ApiOperation(value = "新增菜品")
    public Result<String> insertDish(@RequestBody DishDTO dishDTO) {
        log.info("[新增菜品]{}", dishDTO);

        dishService.insertDish(dishDTO);

        // 构建redis key
        String key = "dish:" + dishDTO.getCategoryId();
        // 清空相关缓存
        cleanRedisByCategoryId(key);
        return Result.success("新增菜品成功");
    }

    /**
     * 菜品分页查询
     *
     * @param dishPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation(value = "菜品分页查询")
    public Result<PageResult> pageListDish(DishPageQueryDTO dishPageQueryDTO) {
        log.info("[菜品分页查询]{}", dishPageQueryDTO);

        PageResult pageResult = dishService.pageListDish(dishPageQueryDTO);

        return Result.success(pageResult);
    }

    /**
     * 根据id查询菜品
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询菜品")
    public Result<DishVO> findDishById(@PathVariable Long id) {
        log.info("[修改数据回显]id:{}", id);

        DishVO dishVO = dishService.findDishById(id);

        return Result.success(dishVO);
    }

    /**
     * 修改菜品信息
     *
     * @param dishDTO
     * @return
     */
    @PutMapping
    @ApiOperation(value = "更新菜品信息")
    public Result<String> updateDishById(@RequestBody DishDTO dishDTO) {

        dishService.updateDishById(dishDTO);

        // 构建redis key
        String key = "dish:" + dishDTO.getCategoryId();
        // 清空相关缓存
        cleanRedisByCategoryId(key);
        return Result.success("修改菜品信息成功");
    }

    /**
     * 根据ids 批量删除菜品
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation(value = "批量删除菜品")
    public Result<String> deleteByIds(@RequestParam List<Long> ids) {
        log.info("[删除菜品]ids{}", ids);

        dishService.deleteDishByIds(ids);

//        for (Long id : ids) {
//            Dish dish = dishMapper.selectDishById(id);
//
//        }
        // 清空相关缓存
        cleanRedisByCategoryId("dish:*");
        return Result.success("删除菜品成功");
    }

    /**
     * 修改菜品状态
     *
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation(value = "修改菜品状态")
    public Result<String> updateDishStatus(@PathVariable Integer status, Long id) {
        log.info("[修改菜品状态]status:{},id:{}", status, id);

        dishService.updateDishStatus(status, id);

        // 清空相关缓存
        cleanRedisByCategoryId("dish:*");
        return Result.success("修改菜品状态成功");
    }

    /**
     * 新增套餐 用于分类展示菜品信息
     *
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    @ApiOperation(value = "按照菜品分类查询菜品信息")
    public Result<List<DishVO>> getDishByCategoryId(Long categoryId) {
        log.info("[新增套餐时列表展示菜品]categoryId:{}", categoryId);

        List<DishVO> dishes = dishService.selectByCategoryId(categoryId);

        return Result.success(dishes);
    }

    /**
     * 根据分类id清空缓存
     *
     * @param keyPatten
     */
    private void cleanRedisByCategoryId(String keyPatten) {
        Set<String> keys = redisTemplate.keys(keyPatten);
        if (keys != null) {
            // 删除相关缓存
            redisTemplate.delete(keys);
        }
    }
}
